package com.drive.controller;

import com.drive.bean.Coach;
import com.drive.constant.MessageConstant;
import com.drive.entity.PageResult;
import com.drive.entity.QueryPageBean;
import com.drive.entity.Result;
import com.drive.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 往
 * 教练管理控制层
 */
@RestController
@RequestMapping("/coach")
public class CoachController {
    @Autowired
    private CoachService coachService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Coach coach, Integer[] studentIds) {
        try {
            coachService.add(coach, studentIds);
        } catch (Exception e) {

            return new Result(false, MessageConstant.ADD_COASH_FAIL);
        }
        return new Result(true, MessageConstant.ADD_COASH_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = coachService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //根据id查询教练
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Coach coach = coachService.findById(id);
        if (coach != null) {
            return new Result(true, MessageConstant.QUERY_COASH_SUCCESS, coach);
        }
        return new Result(false, MessageConstant.QUERY_COASH_FAIL);
    }

    //根据教练id查询对应的所有学员id
    @RequestMapping("/findStudentIdByCoachId")
    public Result findStudentIdByCoachId(Integer id) {
        try {
            List<Integer> studentIds = coachService.findStudentIdByCoachId(id);
            return new Result(true, MessageConstant.QUERY_STUDENT_SUCCESS, studentIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_STUDENT_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result editCoach(@RequestBody Coach coach, Integer[] studentIds) {
        try {
            coachService.edit(coach, studentIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_COASH_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_COASH_SUCCESS);
    }

    //删除
    @RequestMapping("/deleteById")
    public Result delete(Integer id) {
        try {
            coachService.delete(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_COASH_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_COASH_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Coach> coachList = coachService.findAll();
        if(coachList!=null&&coachList.size()>0){
            return new Result(true,MessageConstant.QUERY_COASH_SUCCESS,coachList);
        }
        return new Result(false, MessageConstant.QUERY_COASH_FAIL);
    }
}